package dao

import (
	"fmt"
	"yun_image/entity"
	"yun_image/util"
)

var (
	roles []entity.Role
)

// 从数据库中取出所有的role存到内存里，由于该数据量小变化少查询次数多，这样做能提高性能
func pullAllRoles() error {
	result := DB.Find(&roles)
	if result.Error == nil {
		fmt.Println(util.Green("roles同步成功"))
		return nil
	}
	fmt.Println(util.Red("roles同步失败！！！！"))
	return result.Error
}
func GetRoleById(role *entity.Role, id int) error {
	for _, value := range roles {
		if value.Id == id {
			*role = value
		}
	}
	return nil
}

func UpdateRole(role *entity.Role) error {
	result := DB.Save(role)
	if result.Error == nil {
		return nil
	}
	return result.Error
}
